package com.gz.servlet.servletcontext;

import javax.servlet.ServletContext;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;

public class ContextServlet extends HttpServlet {
    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        doPost(req, resp);
    }

    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        System.out.println("ServletContext的doPost 被调用");

        //1.获取ServletContext对象
        //注: ServletContext是全局的，一个web只有一个
        //所以配置在web.xml里面配置，而不是用注解给单个servlet配置
        ServletContext servletContext = getServletContext();
        //获取website
        String website = servletContext.getInitParameter("website");
        String company = servletContext.getInitParameter("company");
        //获取项目的工程路径
        String contextPath = servletContext.getContextPath();

        //获取项目发布时，正在工作的路径(out)
        // /代表我们的项目发布后的 根路径
        //E:\Java\java_code\JavaWeb\java-web\servlet\out\artifacts\servlet\
        String realPath = servletContext.getRealPath("/");
        System.out.println("项目路径 = "+ contextPath);
        System.out.println("website = " + website + ", company = " + company);
        System.out.println("realPath真实路径 = " + realPath);
    }
}
